<?php
class DataGroupsManager{
	private $SystemGroups = array();
	private $UserGroups = array();
	function __construct(){
	}
	static function swap($bytebuffer){
		$obj = new DataGroupsManager();
		$nof_system_group = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i < $nof_system_group; $i++){
			$obj->SystemGroups[] = DataUtil::readBBin( $bytebuffer ) ;
		}
		$nof_user_group = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i < $nof_user_group; $i++){
			$obj->UserGroups[] = DataUtil::readBBin( $bytebuffer ) ;
		}
		return $obj;
	}
	function addSystemGroup($groupname){
		if(is_array($this->SystemGroups)){
			foreach($this->SystemGroups as $group){
				if(implode(array_map("chr",  $group ))  == $groupname) return;
			}
		}
		$this->SystemGroups[] = unpack('C*', $groupname ) ;
	}
	function addUserGroup($groupname){
		if(is_array($this->UserGroups)){
			foreach($this->UserGroups as $group){
				if(implode(array_map("chr",  $group ))  == $groupname) return;
			}
		}
		$this->UserGroups[] = unpack('C*', $groupname ) ;
	}
	function getSystemGroups(){
		$rs = array();
		if(is_array($this->SystemGroups)){
			foreach($this->SystemGroups as $group){
				$rs[] = implode(array_map("chr",  $group )) ;
			}
		}
		return $rs;
	}
	function getUserGroups(){
		$rs = array();
		if(is_array($this->UserGroups)){
			foreach($this->UserGroups as $group){
				$rs[] = implode(array_map("chr",  $group )) ;
			}
		}
		return $rs;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ;
		if(is_array($this->SystemGroups)){
			foreach($this->SystemGroups as $group){
				$size += 1  + count($group);
			}
		}
		$size += 4 ;
		if(is_array($this->UserGroups)){
			foreach($this->UserGroups as $group){
				$size += 1  + count($group);
			}
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf , count($this->SystemGroups) ) ;
		if(is_array($this->SystemGroups)){
			foreach($this->SystemGroups as $group){
				DataUtil::writeBBin( $buf , $group ) ;
			}
		}
		DataUtil::writeInt32( $buf , count($this->UserGroups) ) ;
		if(is_array($this->UserGroups)){
			foreach($this->UserGroups as $group){
				DataUtil::writeBBin( $buf , $group ) ;
			}
		}
		return  $buf ->_array() ;
	}
}
?>